﻿using System;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace OCDP.Client.Controls
{
    /// <summary>
    /// Logique d'interaction pour ComplexOperator.xaml
    /// </summary>
    public partial class ComplexOperator : UserControl
    {
        private ComplexOperatorViewModel complexOperatorViewModel;
        public ComplexOperator()
        {
            InitializeComponent();
            complexOperatorViewModel = new ComplexOperatorViewModel();
            //SourcePath = "/OCDP.Client;component/Resources/Scan.gif";
            //OperatorName = "Scan Cvs";
            DataContext = complexOperatorViewModel;

            complexOperatorViewModel.PropertyChanged+= PropertyChanged;
        }

        private void PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            PropertyChanged(this, e);
        }

        public string SourcePath
        {
            get 
            { 
                return complexOperatorViewModel!=null ?
                            complexOperatorViewModel.SourcePath
                            : string.Empty; 
            }
            set
            { 
                if (complexOperatorViewModel != null)
                    complexOperatorViewModel.SourcePath = value; 
            }
        }

        public ImageSource ImageSource
        {
            get
            {
                return complexOperatorViewModel!=null ?
                        complexOperatorViewModel.ImageSource
                        :null;
            }
            set
            {
                if (complexOperatorViewModel != null)
                    complexOperatorViewModel.ImageSource = value;
            }
        }

        public string OperatorName
        {
            get
            {
                return complexOperatorViewModel!=null ?
                        complexOperatorViewModel.OperatorName
                        :string.Empty;
            }
            set
            {
                if (complexOperatorViewModel != null)
                    complexOperatorViewModel.OperatorName = value;
            }
        }
    }

    public class ComplexOperatorViewModel : INotifyPropertyChanged
    {
        #region Implementation of INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(PropertyChangedEventArgs eventArgs)
        {
            if (PropertyChanged == null) return;
            PropertyChanged(this, eventArgs);
        }
        #endregion
        private static readonly PropertyChangedEventArgs _ImageSourceChangedEventArgs = new PropertyChangedEventArgs("ImageSource");
        [NotifyParentProperty(true)]
        public ImageSource ImageSource { get; set; }

        private string _sourcePath;
        private static readonly PropertyChangedEventArgs _SourcePathChangedEventArgs = new PropertyChangedEventArgs("SourcePath");
        [NotifyParentProperty(true)]
        public string SourcePath
        {
            get { return _sourcePath; }
            set
            {
                if (string.Compare(value, _sourcePath) != 0)
                {
                    _sourcePath = value;
                    BitmapImage glowIcon = new BitmapImage();
                    glowIcon.BeginInit();
                    glowIcon.UriSource = new Uri("pack://application:,,," + _sourcePath);
                    glowIcon.EndInit();

                    ImageSource = glowIcon;
                    RaisePropertyChanged(_SourcePathChangedEventArgs);
                    RaisePropertyChanged(_ImageSourceChangedEventArgs);
                }
            }
        }

        private static readonly PropertyChangedEventArgs _OperatorNameChangedEventArgs = new PropertyChangedEventArgs("OperatorName");
        private string _operatorName;
        [NotifyParentProperty(true)]
        public string OperatorName
        {
            get { return _operatorName; }
            set
            {
                if (string.Compare(value, _operatorName) != 0)
                {
                    _operatorName = value;
                    RaisePropertyChanged(_OperatorNameChangedEventArgs);
                }
            }
        }
    }
}
